﻿using System;
using System.Runtime.InteropServices;

namespace WCSWaitForm
{
    internal static class WaitFormTools
    {
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        internal static extern int FindWindow(string strClassName, string strWindowName);

        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        internal static extern int FindWindowEx(IntPtr hwndParent, IntPtr hwndChild, string strClassName, string strWindowName);

        /// <summary>
        /// 禁用false/启用true窗口
        /// </summary>
        // [DllImport("user32.dll", CharSet = CharSet.Auto)]
        [DllImport("user32.dll")]// V8产品写法
        //[return: MarshalAs(UnmanagedType.Bool)]// V8产品写法
        internal static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

        /// <summary>
        /// 锁定true/解锁false键盘鼠标
        /// </summary>
        [DllImport("user32.dll")]
        internal static extern void BlockInput(bool bBlock);

        [DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
        internal static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        internal static extern int ReleaseCapture();

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        internal static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        internal static extern int SendMessageA(IntPtr hwnd, int wMsg, int wParam, int lParam);

        /// <summary>
        /// 关闭窗口
        /// </summary>
        internal static void CloseForm(IntPtr intPrt_loading)
        {
            //const int WM_CLOSE = 0x10;   //关闭
            //const uint WM_DESTROY = 0x02;
            //const uint WM_QUIT = 0x12;
            //const int BM_CLICK = 0xF5;  //单击
            //IntPtr Window_Handle = (IntPtr)FindWindow(null, "Microsoft Word");//查找所有的窗体，看看想查找的句柄是否存在，Microsoft Word  句柄              //
            //if (Window_Handle == IntPtr.Zero)   //如果没有查找到相应的句柄
            //{
            //    MessageBox.Show("没有找到窗体");
            //}
            //else    //查找到相应的句柄
            //{
            //    SendMessageA(Window_Handle, WM_CLOSE, 0, 0);   //关闭窗体
            //    IntPtr childHwnd = (IntPtr)FindWindowEx(Window_Handle, IntPtr.Zero, null, "点击");//查找句柄中相应的按钮
            //    if (childHwnd == IntPtr.Zero)
            //    {
            //        MessageBox.Show("没有找到按钮");
            //    }
            //    else
            //    {
            //        SendMessageA(childHwnd, BM_CLICK, 0, 0);   //单击这个句柄中的按钮
            //    }
            //}

            const int WM_CLOSE = 0x10;//关闭
            SendMessageA(intPrt_loading, WM_CLOSE, 0, 0);//关闭窗体            
        }

    }

    public enum ShowWaitSite
    {
        CenterApplication = 0,
        CenterScreen = 1,
        CenterOwner = 2,
        CenterParentForm = 3
    }
}
